package znbkBkfx.BKFX_TKT;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.znbk.znbklibrary.base.AnswerViewPagerAdapter;
import com.example.znbk.znbklibrary.base.BaseFragment;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.R;

import java.util.ArrayList;
import java.util.List;

import znbkBkfx.BkfxActivity;
import znbkBkfx.GetBkfxJson.BkfxPaperEntity;

@SuppressLint("ValidFragment")
public class BkfxTktFragment extends BaseFragment {

    private String QuesBody = "";
    private String SubChildAsk = "";
    private void getEntityInfor(){
        QuesBody = bkfxPaperEntity.getQuesList().get(quesIndex).getQuesBody();

        String GenreName = bkfxPaperEntity.getQuesList().get(quesIndex).getGenreInfo().getGenreName();
        if(GenreName.equals("选词填空")) {
            int ChildListSize = bkfxPaperEntity.getQuesList().get(quesIndex).getChildList().size();
            for (int i = 0; i < ChildListSize; i++) {
                String SubChildAsk_X = bkfxPaperEntity.getQuesList().get(quesIndex).
                        getChildList().get(i).getSubChildList().get(0).getSubChildAsk()+"<br><br>";
                SubChildAsk = SubChildAsk + SubChildAsk_X;
            }
        }else{
            SubChildAsk = bkfxPaperEntity.getQuesList().get(quesIndex).getChildList().get(0).getSubChildList().get(0).getSubChildAsk()+"<br><br>";
        }

    }

    private BkfxPaperEntity bkfxPaperEntity;
    private int quesIndex = -1;
    private CallBackFragmentTkt callBackFragmentTkt;
    public BkfxTktFragment(BkfxPaperEntity bkfxPaperEntity, final int quesIndex, CallBackFragmentTkt callBackFragmentTkt){
        this.bkfxPaperEntity = bkfxPaperEntity;
        this.quesIndex = quesIndex;
        this.callBackFragmentTkt = callBackFragmentTkt;
        getEntityInfor();
    }

    @Override
    protected int createLayout() {
        return R.layout.bkfx_fragment_tkt;
    }

    private WebView mWvQuesBody;
    private ViewPager mVpAnswer;
    private ArrayList<View> list;
    public AnswerViewPagerAdapter mAnswerViewPagerAdapter;

    private RelativeLayout mRlControlView;
    private ScaleButton mSBtnSubmit;

    private LinearLayout mLlResultStuScore;
    @Override
    protected void initView(View view) {

        //显示Ask题干
        mWvQuesBody = view.findViewById(R.id.wv_quesBody);
        mWvQuesBody.setBackgroundColor(0); // 设置背景色
        mWvQuesBody.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        String WvQuesBody = QuesBody+SubChildAsk;
        mWvQuesBody.loadData(WvQuesBody, "text/html", "utf-8");

        //显示Answer信息
        mVpAnswer = view.findViewById(R.id.vp_answerTkt);
        list = new ArrayList<>();
        list.clear();
        RecyclerView mRvAnswer = new RecyclerView(getActivity());
        mRvAnswer.setLayoutManager(new LinearLayoutManager(getActivity()));//必须先设置LayoutManager
        BkfxTktAnswerAdapter mAnswerAdapter = new BkfxTktAnswerAdapter(getActivity(), bkfxPaperEntity, quesIndex);
        mAnswerAdapter.setHasStableIds(true);
        mRvAnswer.setAdapter(mAnswerAdapter);
        int ItemSize  = bkfxPaperEntity.getQuesList().get(quesIndex).getTotalItemCount();
        mRvAnswer.setItemViewCacheSize(ItemSize);
        list.add(mRvAnswer);
        mAnswerViewPagerAdapter = new AnswerViewPagerAdapter(list);
        mVpAnswer.setAdapter(mAnswerViewPagerAdapter);
        bkfxPaperEntity.getPaperAnswer().get(quesIndex).setCurrentSubChildAnswerIndex(0);
        mVpAnswer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                bkfxPaperEntity.getPaperAnswer().get(quesIndex).setCurrentSubChildAnswerIndex(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //显示提交按钮
        mRlControlView = view.findViewById(R.id.rl_controlView);
        mSBtnSubmit  = view.findViewById(R.id.sbtn_submit);
        mSBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bkfxPaperEntity.getPaperAnswer().get(quesIndex).setSubmitState(2);
                hideSBtnSubmit();
            }
        });
        int SubmitState = bkfxPaperEntity.getPaperAnswer().get(quesIndex).getSubmitState();
        if(SubmitState==2){
            mRlControlView.setVisibility(View.GONE);
        }else {
            showSBtnSubmit();
        }

        //学生作答得分
        mLlResultStuScore = view.findViewById(R.id.ll_resultStuScore);

    }

    private void doSubmitRefresh(){
        list.clear();
        RecyclerView mRvAnswer = new RecyclerView(getActivity());
        mRvAnswer.setLayoutManager(new LinearLayoutManager(getActivity()));//必须先设置LayoutManager
        BkfxTktAnswerAdapter mAnswerAdapter = new BkfxTktAnswerAdapter(getActivity(), bkfxPaperEntity, quesIndex);
        mAnswerAdapter.setHasStableIds(true);
        mRvAnswer.setAdapter(mAnswerAdapter);
        int ItemSize  = bkfxPaperEntity.getQuesList().get(quesIndex).getTotalItemCount();
        mRvAnswer.setItemViewCacheSize(ItemSize);
        list.add(mRvAnswer);
        mAnswerViewPagerAdapter = new AnswerViewPagerAdapter(list);
        mVpAnswer.setAdapter(mAnswerViewPagerAdapter);

        int AnsweredCount=0;
        int AnsweredCountTrue=0;
        int SubChildItemSize = bkfxPaperEntity.getQuesList().get(quesIndex).getChildList().get(0).
                getSubChildList().get(0).getSubChildItem().size();
        for(int i=0;i<SubChildItemSize;i++){
            String StuAnswer =  bkfxPaperEntity.getPaperAnswer().get(quesIndex).getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getStuAnswer();
            if(!StuAnswer.equals("")){
                AnsweredCount = AnsweredCount+1;
            }

            String ItemAnswer =  bkfxPaperEntity.getQuesList().get(quesIndex).getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getItemAnswer();
            if(ItemAnswer.equals(StuAnswer)){
                AnsweredCountTrue = AnsweredCountTrue+1;
            }
        }
        //若作答完成则显示答案，记录作答状态，否则亦然
//        if (AnsweredCount == SubChildItemSize) {
            double EachScore = 0;
            String QuesList_QuesID = bkfxPaperEntity.getQuesList().get(quesIndex).getQuesID();
            int PaperQuesSize = bkfxPaperEntity.getPaperQues().size();
            for(int i=0;i<PaperQuesSize;i++){
                int PaperQues_QuesListSize = bkfxPaperEntity.getPaperQues().get(quesIndex).getQuesList().size();
                for(int j=0;j<PaperQues_QuesListSize;j++){
                    String PaperQues_QuesID = bkfxPaperEntity.getPaperQues().get(quesIndex).getQuesList().get(j).getQuesID();
                    if(QuesList_QuesID.equals(PaperQues_QuesID)){
                        EachScore = bkfxPaperEntity.getPaperQues().get(quesIndex).getEachScore();
                        break;
                    }
                }
            }
            double ResultStuScore = AnsweredCountTrue*EachScore;
            showResultStuScore(ResultStuScore);
//        }

        callBackFragmentTkt.submitKgtQuesAnswer();

    }

    private void showSBtnSubmit(){

        ObjectAnimator translationY1 = ObjectAnimator.ofFloat(mRlControlView, View.TRANSLATION_Y, 300f, 0f);
        ObjectAnimator translationY2 = ObjectAnimator.ofFloat(mSBtnSubmit, View.TRANSLATION_Y, 300f, 0f);
        AnimatorSet setShow = new AnimatorSet();
        setShow.playTogether(translationY1,translationY2);
        setShow.setDuration(800);
        setShow.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                mRlControlView.setVisibility(View.VISIBLE);
                mSBtnSubmit.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        setShow.start();
    }

    private void hideSBtnSubmit(){
        if(mSBtnSubmit.getVisibility()==View.VISIBLE) {
            ObjectAnimator translationY1 = ObjectAnimator.ofFloat(mRlControlView, View.TRANSLATION_Y, 0f, 300f);
            ObjectAnimator translationY2 = ObjectAnimator.ofFloat(mSBtnSubmit, View.TRANSLATION_Y, 0f, 300f);
            AnimatorSet setShow = new AnimatorSet();
            setShow.playTogether(translationY1,translationY2);
            setShow.setDuration(800);
            setShow.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    mRlControlView.setVisibility(View.GONE);
                    doSubmitRefresh();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            setShow.start();
        }
    }

    public interface CallBackFragmentTkt {
        void pageMoveNext();
        void submitKgtQuesAnswer();
    }

    private void showResultStuScore(double stuScore){

        String strStuScore = String.valueOf(stuScore);
        if (strStuScore.contains("."))
        {
            String [] newStuScore = strStuScore.split("\\.");
            String front_stuScore = newStuScore[0];
            if(Integer.valueOf(front_stuScore)>=0 && Integer.valueOf(front_stuScore)<=9){
                drawStuScore(front_stuScore);
            }else{
                String str_front_stuScore_0 = front_stuScore.substring(0,1);
                drawStuScore(str_front_stuScore_0);
                String str_front_stuScore_1 = front_stuScore.substring(1,2);
                drawStuScore(str_front_stuScore_1);
            }
            String after_stuScore = newStuScore[1];
            if(Integer.valueOf(after_stuScore)>0){
                ImageView mIv_point = new ImageView(getActivity());
                mIv_point.setImageResource(R.mipmap.stu_score_point);
                mLlResultStuScore.addView(mIv_point);

                ImageView mIv_5 = new ImageView(getActivity());
                mIv_5.setImageResource(R.mipmap.stu_score_5);
                mLlResultStuScore.addView(mIv_5);

            }
        }

        ObjectAnimator translationXShow =  ObjectAnimator.ofFloat(mLlResultStuScore,View.TRANSLATION_X,500f,0f);
        ObjectAnimator alphaShow =  ObjectAnimator.ofFloat(mLlResultStuScore,View.ALPHA,0.6f,1.0f);
        AnimatorSet setShow = new AnimatorSet();
        setShow.playTogether(translationXShow,alphaShow);
        setShow.setInterpolator(new DecelerateInterpolator());
        setShow.setDuration(1600);
        setShow.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                mLlResultStuScore.setVisibility(View.VISIBLE);
                BkfxActivity.mVpBkfx.setScrollble(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mLlResultStuScore.setVisibility(View.GONE);
                callBackFragmentTkt.pageMoveNext();
                BkfxActivity.mVpBkfx.setScrollble(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        setShow.start();

        Log.e("showResultStuScore","=======tlxz=========stuScore==============="+stuScore);

    }

    private void drawStuScore(String score){
        if(score.equals("0")){
            ImageView mIv_0 = new ImageView(getActivity());
            mIv_0.setImageResource(R.mipmap.stu_score_0);
            mLlResultStuScore.addView(mIv_0);
        }else if(score.equals("1")){
            ImageView mIv_1 = new ImageView(getActivity());
            mIv_1.setImageResource(R.mipmap.stu_score_1);
            mLlResultStuScore.addView(mIv_1);
        }else if(score.equals("2")){
            ImageView mIv_2 = new ImageView(getActivity());
            mIv_2.setImageResource(R.mipmap.stu_score_2);
            mLlResultStuScore.addView(mIv_2);
        }else if(score.equals("3")){
            ImageView mIv_3 = new ImageView(getActivity());
            mIv_3.setImageResource(R.mipmap.stu_score_3);
            mLlResultStuScore.addView(mIv_3);
        }else if(score.equals("4")){
            ImageView mIv_4 = new ImageView(getActivity());
            mIv_4.setImageResource(R.mipmap.stu_score_4);
            mLlResultStuScore.addView(mIv_4);
        }else if(score.equals("5")){
            ImageView mIv_5 = new ImageView(getActivity());
            mIv_5.setImageResource(R.mipmap.stu_score_5);
            mLlResultStuScore.addView(mIv_5);
        }else if(score.equals("6")){
            ImageView mIv_6 = new ImageView(getActivity());
            mIv_6.setImageResource(R.mipmap.stu_score_6);
            mLlResultStuScore.addView(mIv_6);
        }else if(score.equals("7")){
            ImageView mIv_7 = new ImageView(getActivity());
            mIv_7.setImageResource(R.mipmap.stu_score_7);
            mLlResultStuScore.addView(mIv_7);
        }else if(score.equals("8")){
            ImageView mIv_8 = new ImageView(getActivity());
            mIv_8.setImageResource(R.mipmap.stu_score_8);
            mLlResultStuScore.addView(mIv_8);
        }else if(score.equals("9")){
            ImageView mIv_9 = new ImageView(getActivity());
            mIv_9.setImageResource(R.mipmap.stu_score_9);
            mLlResultStuScore.addView(mIv_9);
        }
    }

}